import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:leader_help/utils/util.dart';

import 'index.dart';

class DragablePage extends StatelessWidget {
  DragablePage({Key? key, required this.child, required this.onTap})
      : super(key: key);
  final VoidCallback onTap;
  final Widget child;
  final controller = Get.put(DragableController());
  final state = Get.find<DragableController>();
  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    return Obx(
      () => (!state.isShowFloatObs.value)
          ? const SizedBox()
          : Positioned(
              right: state.right.value,
              bottom: state.bottom.value,
              // width: state.itemSize,
              // height: _height.value,
              child: GestureDetector(
                onTap: onTap,
                child: Draggable(
                  feedback: child,
                  child: child,
                  onDragUpdate: (details) {
                    var _maxX = state.right.value;
                    var _maxY = state.bottom.value;

                    _maxX -= details.delta.dx;
                    _maxY -= details.delta.dy;
                    if (_maxX < gap12) {
                      _maxX = gap12;
                    }
                    if (_maxX > Get.width - gap12 - state.itemSize) {
                      _maxX = Get.width - gap12 - state.itemSize;
                    }
                    if (_maxY <= Get.bottomBarHeight) {
                      _maxY = Get.bottomBarHeight;
                    }
                    if (_maxY >=
                        Get.height - SizeFit.navBarHeight - state.itemSize) {
                      _maxY =
                          Get.height - SizeFit.navBarHeight - state.itemSize;
                    }
                    state.right.value = _maxX;
                    state.bottom.value = _maxY;
                  },
                  onDragEnd: (details) {
                    if (state.right.value < gap12) {
                      state.right.value = gap12;
                    }
                    if (state.right.value < (Get.width - state.itemSize) / 2) {
                      state.right.value = gap12;
                    } else {
                      state.right.value = Get.width - gap12 - state.itemSize;
                    }
                    if (state.bottom.value < Get.bottomBarHeight) {
                      state.bottom.value = Get.bottomBarHeight;
                    }
                    if (state.bottom.value >=
                        Get.height - SizeFit.navBarHeight - state.itemSize) {
                      state.bottom.value =
                          Get.height - SizeFit.navBarHeight - state.itemSize;
                    }
                  },
                ),
              ),
            ),
    );
  }
}
